package cn.lan.generator.manager;

import cn.hutool.json.JSONUtil;
import cn.lan.generator.factory.EntityServiceFactory;
import cn.lan.generator.vo.MenuVo;
import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.*;
import cn.lan.generator.service.impl.*;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;
import cn.lan.generator.utils.StringUtil;
import com.alibaba.fastjson.JSONArray;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class EntityRelationManager {

    /**
     * 菜单
     */
    public static List<MenuVo> ENTITY_MENUS=new ArrayList<>();

    /**
     * 创建所有entity相关的文件
     * @param projInfo
     * @param tableName
     * @param tableComment
     * @param result
     * @param dbType
     * @param schema
     * @throws Exception
     */
    public void createEntityBusiFile(ProjInfo projInfo, String tableName,String tableComment, ResultSet result,
                          String dbType,String schema) throws Exception {

        EntityInfo entityInfo = EntityServiceFactory.getEntityService(dbType).getEntityInfo(result, tableName, tableComment);

        CreateFile createEntity = new CreateEntityImpl();
        createEntity.writeFile(entityInfo, projInfo);

        CreateFile createFile = new CreateEntityDtoImpl();
        createFile.writeFile(entityInfo,projInfo);

        CreateFile createEntityListDto= new CreateEntityListDtoImpl();
        createEntityListDto.writeFile(entityInfo,projInfo);

        CreateFile createEntityVo = new CreateEntityVoImpl();
        createEntityVo.writeFile(entityInfo,projInfo);

        CreateMapperXML createXML = new CreateMapperXmlImpl();
        createXML.writeFile(entityInfo, projInfo, schema);

        CreateFile createMapper = new CreateMapperFileImpl();
        createMapper.writeFile(entityInfo, projInfo);

        CreateFile createService = new CreateServiceFileImpl();
        createService.writeFile(entityInfo, projInfo);

        CreateFile createServiceImpl = new CreateServiceImplFileImpl();
        createServiceImpl.writeFile(entityInfo, projInfo);

        CreateFile createController = new CreateControllerImpl();
        createController.writeFile(entityInfo, projInfo);

        if(projInfo.isThymeleaf()){
            CreateFile createViewController = new CreateViewControllerImpl();
            createViewController.writeFile(entityInfo, projInfo);

            CreateFile createEntityPage = new CreateEntityPageImpl();
            createEntityPage.writeFile(entityInfo,projInfo);

            String lowerEntityName = StringUtil.toLowerCaseFirstOne(entityInfo.getEntityName());
            MenuVo menuVo = new MenuVo();
            menuVo.setHref(lowerEntityName+"/list");
            menuVo.setTitle(entityInfo.getTableComment());
            ENTITY_MENUS.add(menuVo);
        }
    }

    /**
     * nav.js
     * @param projInfo
     * @param suffixPath
     */
    public static void writeNavJsFile(ProjInfo projInfo,String suffixPath){
        String outFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getWebServer(), Constant.PathType.resource_static, suffixPath);
        String content = "var navs = MENUS_LIST;";
        content = content.replace("MENUS_LIST", JSONUtil.formatJsonStr(JSONArray.toJSONString(ENTITY_MENUS)));
        FileUtil.writeFile(projInfo,content,outFilePath);
    }
}
